package com.vcredit.creditcat.service;

import java.util.*;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.model.CustProductItem;
import com.vcredit.creditcat.model.MerchantProduct;
import com.vcredit.creditcat.persistence.CustProductItemMapper;
import com.vcredit.creditcat.persistence.base.MerchantProductMapper;
import com.vcredit.creditcat.util.Utility;

/**
 * 
 **/
@Service
public class MerchantProductService {
	@Autowired
	private MerchantProductMapper mapper;

	@Autowired
	private CustProductItemMapper custProductItemMaper;

	@CacheEvict(key = "'MerchantProduct'", value = "product")
	public void insert(MerchantProduct obj) {
		mapper.insert(obj);
	}

	@CacheEvict(key = "'MerchantProduct'", value = "product")
	public void update(MerchantProduct obj) {
		mapper.update(obj);
	}

	public List<MerchantProduct> findAllList() {
		List<MerchantProduct> merchantProducts = new ArrayList<MerchantProduct>();
		Map<Long, MerchantProduct> map = this.findAll();
		for (Entry<Long, MerchantProduct> entry : map.entrySet()) {
			// TODO 暂时排除卡卡贷
			if(entry.getKey() == 1) {
				continue;
			}
			merchantProducts.add(entry.getValue());
		}
		return merchantProducts;
	}

	public List<MerchantProduct> findAllMerchantProduct() {
		List<MerchantProduct> products = mapper.findAll();
		Iterator<MerchantProduct> iterator = products.iterator();
		while (iterator.hasNext()) {
			MerchantProduct product = iterator.next();
			if (product.getMerchantId() == 0) {
				iterator.remove();
			}
		}
		return  products;
	}

	@Cacheable(key = "'MerchantProduct'", value = "product")
	public Map<Long, MerchantProduct> findAll() {
		Map<Long, MerchantProduct> merchantProducts = new LinkedHashMap<>();
		for (MerchantProduct product : mapper.findAll()) {
			if (product.getMerchantId() > 0)
				merchantProducts.put(product.getProductId(), product);
		}
		return merchantProducts;
	}

	@SuppressWarnings("unused")
	private List<MerchantProduct> find(MerchantProduct obj) {
		return mapper.find(obj);
	}

	public MerchantProduct findById(Long productId) {
		return this.findAll().get(productId);
	}

	public List<MerchantProduct> findHotProduct(Boolean isHot) {
		List<MerchantProduct> products = new ArrayList<MerchantProduct>();
		Map<Long, MerchantProduct> productMap = this.findAll();
		for (Long key : productMap.keySet()) {
			MerchantProduct product = productMap.get(key);
			if (product.getIsHot() == isHot)
				products.add(product);
		}
		return products;
	}

	public List<MerchantProduct> findTechnialProduct(Boolean isTechnial) {
		List<MerchantProduct> products = new ArrayList<MerchantProduct>();
		Map<Long, MerchantProduct> productMap = this.findAll();
		for (Long key : productMap.keySet()) {
			MerchantProduct product = productMap.get(key);
			if (product.getIsTechnical() == isTechnial)
				products.add(product);
		}
		return products;
	}

	public List<CustProductItem> findByProcessId(Long processId) {
		return custProductItemMaper.findByProcessId(processId, null);
	}

	public List<CustProductItem> findIsAuthByProcessId(Long processId, Boolean isAuth) {
		return custProductItemMaper.findByProcessId(processId, isAuth ? 1 : 0);
	}

	public List<MerchantProduct> findByIds(Long[] productIds) {
		return mapper.findByIds(productIds);
	}
	
	public List<MerchantProduct> findBySucCodeAndTechnical(int successCode, boolean isTechnical) {
		return mapper.findBySucCodeAndTechnical(successCode, isTechnical);
	}
	
	public List<MerchantProduct> findAuthByProcessIdAndTechnical(Long processId, Boolean isAuth) {
		List<MerchantProduct> list = new ArrayList<MerchantProduct>();
		List<CustProductItem> productItems = this.findIsAuthByProcessId(processId, isAuth);
		Map<Long, MerchantProduct> allProduct = this.findAll();
		if(Utility.isNullOrEmpty(productItems)) {
			return list;
		}
		for(CustProductItem productItem : productItems) {
			MerchantProduct merchantProduct = allProduct.get(productItem.getProductId());
			if(merchantProduct.getIsTechnical()) {
				list.add(merchantProduct);
			}
		}
		return list;
	}
	public List<MerchantProduct> findSpeedloanProductsByVersion(String appDesc,String mobile){
		String version ="default";
		if(StringUtils.isNotBlank(appDesc)) {
			version = appDesc;
		}
		return mapper.findFastProductsByVersion(version,mobile);
	}

	public List<MerchantProduct> findHotProducts(String appDesc,Integer start,Integer limit){
		String version ="default";
		if(StringUtils.isNotBlank(appDesc)) {
			version = appDesc;
		}
		return mapper.findHotProducts(version,start,limit);
	}
}